import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Deom4 {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入日期");
        System.out.println("格式****-**-**");
        String time = scanner.nextLine();

        System.out.println("请输入金额");
        int money = scanner.nextInt();

        System.out.println("请输入收入/支出");
         scanner.nextLine();
          String In_Or_Ex = scanner.nextLine();

        System.out.println("请输入备注");
        String remark = scanner.nextLine();

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/BillList?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("88800662");

        Connection connection = dataSource.getConnection();

        String sql = ("insert into billist value(null,?,?,?,?)");
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,time);
        statement.setInt(2,money);
        statement.setString(3,In_Or_Ex);
        statement.setString(4,remark);

        int n = statement.executeUpdate();
        System.out.println("row " + n);
        statement.close();
        connection.close();

        System.out.println("添加成功");
    }
}
